package com.own.dondeestoy.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;
import android.util.Log;

import com.own.dondeestoy.core.Card;
import com.own.dondeestoy.data.DAO;
import com.own.dondeestoy.data.Helper;


public class CardDao extends DAO<Card>{

	private static final String LOGTAG = "CardDao";	
	
	public CardDao(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String getTableName() {
		return "Cards";
	}
	
	@Override
	public Card buildNewEntity(Cursor cursor) {
		Card card = new Card();
		card.setId(cursor.getInt(0));
		card.setName(cursor.getString(1));
		card.setDescription(cursor.getString(2));
		card.setLng(cursor.getString(3));
		card.setLat(cursor.getString(4));
		card.setAddress(cursor.getString(5));
		card.setPhoto(cursor.getString(6));
		card.setCardtypeid(cursor.getInt(7));
				
		return card;
	}

	public Card getPoint(String address) {
		SQLiteDatabase db = null;
		Card entity = new Card();
		try{
			String[] filter = new String[1];
			filter[0] = address;
			if(!_session.isOpen())
				_session.Open();
			
			db = _session.Connection();
			Cursor cursor = db.rawQuery("SELECT id, name, description, lng, lat, address, photo, cardtypeid FROM Cards WHERE address = ? ", filter);
			
			cursor.moveToFirst();
			entity = buildNewEntity(cursor);
		    cursor.close();
		    db.close();		    
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
			throw ex;
		}		
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
		return entity;
	}
	
	public Card getPoint(double latitude, double longitude) {
		SQLiteDatabase db = null;
		Card entity = new Card();
		try{
			String[] filter = new String[2];
			filter[0] = Double.toString(latitude);
			filter[1] = Double.toString(longitude);
			if(!_session.isOpen())
				_session.Open();
			
			db = _session.Connection();
			Cursor cursor = db.rawQuery("SELECT id, name, description, lng, lat, address, photo, cardtypeid FROM Cards WHERE lat = ? AND lng = ? ", filter);
			
			cursor.moveToFirst();
			entity = buildNewEntity(cursor);
		    cursor.close();
		    db.close();		    
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
			throw ex;
		}		
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
		return entity;
	}
	
	public List<Card> getByIds(List<Integer> ids) {
		SQLiteDatabase db = null;
		try{
			List<Card> list = new ArrayList<Card>();
			if(!_session.isOpen())
				_session.Open();
									
			db = _session.Connection();
			Cursor cursor = db.rawQuery("SELECT id, name, description, lng, lat, address, photo, cardtypeid FROM Cards WHERE id IN " + Helper.makeQueryInInt(ids), null);
			
			cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	list.add(buildNewEntity(cursor));
		    	cursor.moveToNext();
		    }
		    cursor.close();
		    db.close();
		    return list;
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
			throw ex;
		}		
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
	}
	
	public List<Card> getCardByCardTypes(List<Integer> ids) {
		SQLiteDatabase db = null;
		try{
			List<Card> list = new ArrayList<Card>();
						
			if(!_session.isOpen())
				_session.Open();
		
			db = _session.Connection();
			Cursor cursor = db.rawQuery("SELECT id, name, description, lng, lat, address, photo, cardtypeid FROM Cards WHERE cardtypeid IN " + Helper.makeQueryInInt(ids), null);
			
			cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	list.add(buildNewEntity(cursor));
		    	cursor.moveToNext();
		    }
		    cursor.close();
		    db.close();
		    return list;
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
			throw ex;
		}		
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
	}
	
}
